package proxyCommand;

import java.util.ArrayList;

public class GUIProxy {
	public GlobalObjectId globalObjectId;
	private static ArrayList<GUIProxy> guis = new ArrayList<GUIProxy>();
	
	private GUIProxy(GlobalObjectId globalObjectId) {
		this.globalObjectId = globalObjectId;
	}
	
	public static ArrayList<GUIProxy> getGUIs() {
		return guis;
	}
	
//	public static GUIProxy getSingleton() {
//		assert singleton != null;
//		return singleton;
//	}
	
	public static GlobalObjectId addGlobalObjectId(GlobalObjectId globalObjectId) {
//		int id = globalObjectId.getPortNumber().getValue();
//		globalObjectId.getPortNumber().setValue(id + guis.size() + 1);
		guis.add(new GUIProxy(globalObjectId));
		return globalObjectId;
	}
	
	public static void removeGlobalObjectId(GlobalObjectId globalObjectId) {
		for(int i = 0; i < guis.size(); i++) {
			if(guis.get(i).globalObjectId.equals(globalObjectId)) {
				guis.remove(i);
			}
		}
	}
	
	public static void printToTracePanelAll(String result) {
		for(GUIProxy g : guis) {
			g.printToTracePanel(result);
		}
	}
	
	private void printToTracePanel(Object p0) {
    	String[] parameterTypeNames = new String[1];
        parameterTypeNames[0] = "java.lang.Object";
        Object[] actualParameters = new Object[1];
        actualParameters[0] = p0;
        Command command = new Command(globalObjectId.getLocalObjectId(), "gui.Main.GUI", "printToTracePanel", parameterTypeNames, actualParameters, false);
        PeerCommunicator.getSingleton().sendASynchronous(globalObjectId, command);
    }
	
	public static void updateNodeListAll() {
		for(GUIProxy g : guis) {
			g.updateNodeList();
		}
	}
	
	private void updateNodeList() {
		String[] parameterTypeNames = new String[0];
        Object[] actualParameters = new Object[0];
        Command command = new Command(globalObjectId.getLocalObjectId(), "gui.Main.GUI", "updateNodeList", parameterTypeNames, actualParameters, false);
        PeerCommunicator.getSingleton().sendASynchronous(globalObjectId, command);
	}

	public static void closeGuiAll() {
		for(GUIProxy g : guis) {
			g.closeGui();
		}
	}
	
	private void closeGui() {
		String[] parameterTypeNames = new String[0];
        Object[] actualParameters = new Object[0];
        Command command = new Command(globalObjectId.getLocalObjectId(), "gui.Main.GUI", "closeGui", parameterTypeNames, actualParameters, false);
        PeerCommunicator.getSingleton().sendASynchronous(globalObjectId, command);
	}
}
